package com.aaa.service.mht.impl;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.InsertAnn;
import com.aaa.aspect.UpdateAnn;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.mht.TestPlanMht;
import com.aaa.dao.mht.TestPlanMhtDao;
import com.aaa.service.mht.TestPlanMhtService;
import com.aaa.utils.MyTools;
import com.github.pagehelper.PageHelper;
import lombok.extern.java.Log;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 心理健康MHT计划表(TestPlanMht)表服务实现类
 *
 * @author makejava
 * @since 2022-09-01 08:03:08
 */
@Service("testPlanMhtService")
@Log
public class TestPlanMhtServiceImpl implements TestPlanMhtService {
    @Resource
    private TestPlanMhtDao testPlanMhtDao;

    @Override
    public List<TestPlanMht> queryAllMhtPlan(TestPlanMht testPlanMht, PageSortEntity pageSortEntity) {
        PageHelper.startPage(pageSortEntity.getPage(), pageSortEntity.getLimit());
        List<TestPlanMht> testPlanMhtList = testPlanMhtDao.queryAllMhtPlan(testPlanMht);
        return testPlanMhtList;
    }


    /**
     * 新增数据
     *
     * @param testPlanMht 实例对象
     * @return 实例对象
     */
    @InsertAnn
    @Override
    public TestPlanMht save(TestPlanMht testPlanMht) {
        while (true) {
            TestPlanMht testPlanMhtNew = new TestPlanMht();
            //此处，生成不重复的随机数，不足6为的前补零
            String sixRandom = MyTools.getSixRandom();
            testPlanMhtNew.setTestCode(sixRandom);
            //判断是否跟数据库已经存在的邀请码重复
            List<TestPlanMht> testPlanMhtList = testPlanMhtDao.queryByAllView(testPlanMhtNew);
            log.info("生成的随机数，跟数据库是否一致校验" + sixRandom);
            if (ObjectUtil.isEmpty(testPlanMhtList) || testPlanMhtList.size() == 0) {
                //退出循环之前，将合法的随机数保存到实体对象中testPlanFpa
                testPlanMht.setTestCode(sixRandom);
                break;
            }
        }
        this.testPlanMhtDao.save(testPlanMht);
        return testPlanMht;
    }

    /**
     * 修改数据
     *
     * @param testPlanMht 实例对象
     * @return 实例对象
     */
    @UpdateAnn
    @Override
    public Integer update(TestPlanMht testPlanMht) {
        int update = testPlanMhtDao.update(testPlanMht);
        return update;
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public Boolean deleteById(Integer id) {
        Boolean i = testPlanMhtDao.deleteById(id);
        return i;
    }

    @Override
    public Integer delTestPlanBatch(int[] id) {
        Integer integer = testPlanMhtDao.delTestPlanBatch(id);
        return integer;
    }


    @Override
    public Integer repetitionName(TestPlanMht testPlanMht) {
        Integer integer = testPlanMhtDao.repetitionName(testPlanMht);
        return integer;
    }


}
